<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$title}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__ADMIN_PUBLIC__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__ADMIN_PUBLIC__/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <!--<div class="layui-card-header">{$title}</div>-->
                <div class="layui-card-body">
                    <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
                        <button class="layui-btn" id="add-group"><i class="layui-icon"></i> 新增用户组</button>
                    </div>
                    <table class="layui-hide" id="test-table-group" lay-filter="test-table-group"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="addGroup">
    <form class="layui-form" action="" style="padding-top: 20px;">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">用户组名称：</label>
            <div class="layui-input-inline" style="width: 350px;">
                <input type="text" name="title" class="layui-input" placeholder="用户组名称" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">用户组状态：</label>
            <div class="layui-input-inline">
                <input type="checkbox" checked="" name="status" lay-skin="switch" lay-text="启用|禁用">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">拥有权限：</label>
            <div class="layui-input-inline" style="width: 400px;">
                <table class="layui-hide" lay-data="{id:'idTest'}" id="test-table-rule" lay-filter="test-table-rule"></table>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="butClick">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="__ADMIN_PUBLIC__/layui/layui.js"></script>
<script>
    layui.config({
        base: '__ADMIN_PUBLIC__/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        treetable:'treetable-lay/treetable'
    }).use(['index', 'table','form','layer','treetable'], function(){
        var $ = layui.jquery,
            form = layui.form,
            treetable = layui.treetable,
            table = layui.table;

        var groupTable = function(){
            layer.load(2);
            table.render({
                elem: '#test-table-group'
                ,url: 'authGroup'
                ,cellMinWidth: 150
                ,page: true
                ,cols: [[
                    {field:'id', title:'ID',align:'center'}
                    ,{field:'title', title:'用户组名称',align:'center'}
                    ,{field:'status', title:'状态',align:'center',templet:function (d) {
                        if (d.status == "0") {
                            return '<span class="layui-btn layui-btn-xs layui-btn-disabled">禁用</span>';
                        }else if (d.status == "1") {
                            return '<span class="layui-btn layui-btn-xs  layui-btn-normal">启用</span>';
                        }
                    }}
                    ,{field:'create_time', title:'创建时间', width:200,align:'center'}
                    ,{field:'right', title:'操作', toolbar: '#barDemo',align:'center'}
                ]],
                done: function () {
                    layer.closeAll('loading');
                }
            });
        };
        groupTable();


        var ruleTable = function (rids = '') {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'id',
                treePidName: 'pid',
                treeDefaultClose: false,
                treeLinkage: false,
                elem: '#test-table-rule'
                ,url: 'getAuthRuleTree?rids='+rids
                ,page: false,
                id:'idTest'
                ,cols: [[
                    {type: 'checkbox'},
                    {field:'title', title:'权限名称',align:'left'}
                ]]
            });
        };
        // ruleTable();


        var openHtml;
        $('#add-group').on('click',function () {
            ruleTable();
            //页面层
            openHtml =  layer.open({
                type: 1,
                title:'新增用户组',
                skin: 'layui-layer-rim', //加上边框
                area: ['700px', '450px'], //宽高
                content:$('#addGroup').html()
            });
            form.render();
        });

        form.on('submit(butClick)',function (obj) {
            var checkStatus = table.checkStatus('idTest');
            // console.log(checkStatus.data);
            // console.log(obj);
            var checkAuth = checkStatus.data;
            var title = obj.field.title;
            var status = obj.field.status;
            $.post("addUserGroup",{title:title,status:status,checkAuth:checkAuth},function (response) {
                // console.log(response);
                if(response.code == 0){
                    layer.msg(response.msg,{icon:6,time:1000},function () {
                        layer.close(openHtml);
                        groupTable();
                    });
                }else{
                    layer.msg(response.msg,{icon:5,anim: 6});
                }
            });
            return false;
        });

        table.on('tool(test-table-group)',function (obj) {
            console.log(obj);
            var data = obj.data;
            if(obj.event == 'edit'){
                // $.post('',{},function () {
                //
                // });
                ruleTable(data.rules);
                //页面层
                openHtml =  layer.open({
                    type: 1,
                    title:'编辑用户组',
                    skin: 'layui-layer-rim', //加上边框
                    area: ['700px', '450px'], //宽高
                    content:$('#addGroup').html(),
                    success:function (layero,index) {
                        layero.find("input[name='title']").val(data.title);
                        if(data.status == 1){
                            layero.find("input[name='status']").prop('checked','true');
                        }else{
                            layero.find("input[name='status']").prop('checked');
                        }
                    }
                });
                form.render();
            }else if(obj.event == 'del'){

            }
        });
    });
</script>
</body>
</html>